package com.ruoyi.system.service;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.config.BigDataConfig;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.core.domain.LatLng;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.http.BigDataResponse;
import com.ruoyi.common.utils.http.HttpRequest;
import com.ruoyi.system.domain.request.electricFence.PoiCitySearchParam;
import com.ruoyi.system.domain.vo.common.DeviceGps;
import com.ruoyi.system.utils.ComLbsUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 描述 全局通用服务
 * 作者 ZhangPing
 * 创建时间 2023/8/16 11:25
 */
@Service
public class CommonService {

    @Autowired
    private RedisCache redisCache;
    @Autowired
    private BigDataConfig bigDataConfig;

    /**
     * 获取缓存中车辆在线数据
     */
    public Map<String, DeviceGps> getDeviceOnlineDataFromCache(Collection<String> deviceIds) {
        Map<String, DeviceGps> gpsMap = redisCache.getCacheMap("allLastGps");
        if (ObjectUtils.isEmpty(deviceIds) || ObjectUtils.isEmpty(gpsMap)) return gpsMap;
        Map<String, DeviceGps> resultMap = new HashMap<>();
        for (String deviceId : gpsMap.keySet()) {
            if (deviceIds.contains(deviceId)){
                resultMap.put(deviceId, gpsMap.get(deviceId));
            }
        }
        return resultMap;
    }

    public Map<String, DeviceGps> getDeviceOnlineDataFromCache() {
        return getDeviceOnlineDataFromCache(null);
    }

    /**
     * 定时从大数据更新车辆数据
     */
    public Map<String, DeviceGps> getDeviceOnlineDataFromService(Collection<String> deviceIds){
        Map<String, DeviceGps> cache = new HashMap<>();
        //远程调用接口，获取设备在线信息
        BigDataResponse<List<JSONObject>> response;
        if (ObjectUtils.isEmpty(deviceIds)){
            //查询全部数据
            response = HttpRequest.getBigData(bigDataConfig.getApiUrl() + "/gpsData/queryAllLastGps", null, new TypeReference<List<JSONObject>>(){}.getType());
        } else {
            //不为空时则查指定设备
            response = HttpRequest.postJsonBigData(bigDataConfig.getApiUrl() + "/gpsData/queryListLastGps",
                    new JSONObjectPro().put("dvoList", deviceIds), new TypeReference<List<JSONObject>>(){}.getType());
        }
        if (response.isSuccess()) {
            //此处没有在接收的时候直接转换，是因为大数据有些字段嵌套太深了
            for (JSONObject item : response.getData()) {
                DeviceGps deviceGps = ComLbsUtils.bigDataGpsFormat(item);
                cache.put(deviceGps.getDvo(), deviceGps);
                redisCache.setCacheMapValue("allLastGps", deviceGps.getDvo(), deviceGps);
            }
        }
        return cache;
    }

    public Map<String, DeviceGps> getDeviceOnlineDataFromService(){
        return getDeviceOnlineDataFromService(null);
    }

    /**
     * 经纬度转地址(高德)
     */
    public String latLngToAddress(LatLng latLng) {
        String url = bigDataConfig.getPoiServiceUrl() + "/bigdata/poiData/getMinAdsByPoi?lng=" + latLng.getLng() + "&lat=" + latLng.getLat();
        BigDataResponse<JSONObject> response = HttpRequest.getBigData(url, null, JSONObject.class);
        return response.isSuccess() ? response.getData().getString("address") : "";
    }

    /**
     * 经纬度转地址(84)
     */
    public String latLngToAddress84(LatLng latLng) {
        String url = bigDataConfig.getPoiServiceUrl() + "/bigdata/poiData/getMinAdsBy84Poi?lng=" + latLng.getLng() + "&lat=" + latLng.getLat();
        BigDataResponse<JSONObject> response = HttpRequest.getBigData(url, null, JSONObject.class);
        return response.isSuccess() ? response.getData().getString("address") : "";
    }

    /**
     * 位置查询
     */
    public JSONArray matchQuery(PoiCitySearchParam param) {
        BigDataResponse<JSONArray> response = HttpRequest.getBigDataNoEncode(bigDataConfig.getPoiServiceUrl() + "/bigdata/poiData/matchQuery", param.toJson(), JSONArray.class);
        if (response.isSuccess() && ObjectUtils.isNotEmpty(response.getDetail())) {
            return response.getDetail();
        } else {
            return new JSONArray();
        }
    }

}
